<?php
namespace app\mapi\controller;
use app\mapi\model\UserListModel;
use app\mapi\model\MallOrderModel;
use app\mapi\model\MallFormsModel;
use com\Aes;

/**
 * 用户中心控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class Users extends Base{

    # 用户登录
    public function userLogin(){
        if(request()->isPost()){
            $User = input('post.user','','trim');
            $Pass = input('post.pass','','trim');

            if(empty($User)){
                $this->ShowAjax(2, '请输入登录帐号');
            }

            if(empty($Pass)){
                $this->ShowAjax(2, '请输入登录密码');
            }

            $UserList = new UserListModel;

            $FindUser = $UserList->mInfo(['user_account'=>$User]);

            if(!empty($FindUser)){
                if($FindUser['user_pass'] == md5UsersPass($Pass)){
                    if($FindUser['user_state'] == 1){
                        $Aes = new Aes();

                        # Aes加密
                        $IDS = $Aes->encrypt($FindUser['user_id']);

                        # 生成令牌(备用)
                        # $Token = md5UsersToken($FindUser['user_id'], $Time, rand(100000, 999999));

                        $this->ShowAjax(1, '登录成功', $IDS);
                    }else{
                        $this->ShowAjax(2, '帐号已被禁用');
                    }
                }else{
                    $this->ShowAjax(2, '密码输入错误');
                }
            }else{
                $this->ShowAjax(2, '帐号输入错误');
            }
        }
    }

    # 个人信息
    public function userCenter(){
        if(request()->isPost()){
            $Token = input('post.token','','trim');

            $FindUser = $this->checkLogin($Token);

            unset($FindUser['user_id']);

            $FindUser['user_create'] = date('Y-m-d H:s:i', $FindUser['user_create']);

            $this->ShowAjax(1, '查询成功', $FindUser);
        }
    }

    # 绑定手机
    public function userMobile(){
        if(request()->isPost()){
            $Token = input('post.token','','trim');
            $Mobile = input('post.mobile','','trim');

            $FindUser = $this->checkLogin($Token);

            if(!empty($Mobile)){
                $UserList = new UserListModel;

                $Back = $UserList->mEdit(['user_id'=>$FindUser['user_id']], ['user_mobile'=>$Mobile]);

                $this->ShowAjax($Back['code'], $Back['msgs']);
            }else{
                $this->ShowAjax(2, '请输入手机号码');
            }
        }
    }

    # 订单列表
    public function orderList(){
        if(request()->isPost()){
            $Token = input('post.token','','trim');
            $Types = input('post.type','','intval') ? input('post.type','','intval') : 0;
            $Pages = input('post.page','','intval') ? input('post.page','','intval') : 0;
            $Limit = input('post.limit','','intval') ? input('post.limit','','intval') : 10;

            $FindUser = $this->checkLogin($Token);

            $Where = [];
            $Where['order_user'] = $FindUser['user_id'];

            if($Types > 0){
                $Where['order_state'] = $Types;
            }

            $MallOrder = new MallOrderModel;

            $ListOrder = $MallOrder->mList($Where, 'order_id,order_number,order_amount,order_total,order_need,order_name,order_mobile,order_address,order_state,order_pay,order_send,order_end,order_create', $Pages, $Limit);

            if(!empty($ListOrder)){
                $MallForms = new MallFormsModel;

                foreach($ListOrder as $k => $v){
                    $ListOrder[$k]['order_states'] = $this->orderState($ListOrder[$k]['order_state']);
                    $ListOrder[$k]['order_pay'] = $v['order_pay'] > 0 ? date('Y-m-d H:i:s', $v['order_pay']) : '--';
                    $ListOrder[$k]['order_send'] = $v['order_send'] > 0 ? date('Y-m-d H:i:s', $v['order_send']) : '--';
                    $ListOrder[$k]['order_end'] = $v['order_end'] > 0 ? date('Y-m-d H:i:s', $v['order_end']) : '--';
                    $ListOrder[$k]['order_create'] = date('Y-m-d H:i:s', $v['order_create']);

                    # 订单商品信息
                    $FindForms = $MallForms->mAll(['forms_order'=>$v['order_id']], 'forms_good,forms_name,forms_imgs,forms_spec,forms_price,forms_total,forms_need,forms_sale,forms_number');

                    $ListOrder[$k]['order_forms'] = $FindForms;
                }

                $Code = 1;
                $Msgs = '查询成功';
            }else{
                $Code = 2;
                $Msgs = '暂无订单';
            }

            $this->ShowAjax($Code, $Msgs, $ListOrder);
        }
    }

    # 订单详情
    public function orderDetail(){
        if(request()->isPost()){
            $Token = input('post.token','','trim');
            $Order = input('post.order_id','','intval');

            $FindUser = $this->checkLogin($Token);

            if(empty($Order)){
                $this->ShowAjax(2, '缺少order_id参数');
            }

            $MallOrder = new MallOrderModel;

            $FindOrder = $MallOrder->mInfo(['order_id'=>$Order, 'order_user'=>$FindUser['user_id']], 'order_id,order_number,order_amount,order_total,order_need,order_name,order_mobile,order_address,order_state,order_pay,order_send,order_end,order_create');

            if(!empty($FindOrder)){
                
                $FindOrder['order_states'] = $this->orderState($FindOrder['order_state']);
                $FindOrder['order_pay'] = $FindOrder['order_pay'] > 0 ? date('Y-m-d H:i:s', $FindOrder['order_pay']) : '--';
                $FindOrder['order_send'] = $FindOrder['order_send'] > 0 ? date('Y-m-d H:i:s', $FindOrder['order_send']) : '--';
                $FindOrder['order_end'] = $FindOrder['order_end'] > 0 ? date('Y-m-d H:i:s', $FindOrder['order_end']) : '--';
                $FindOrder['order_create'] = date('Y-m-d H:i:s', $FindOrder['order_create']);

                $MallForms = new MallFormsModel;

                # 订单商品信息
                $FindForms = $MallForms->mAll(['forms_order'=>$FindOrder['order_id']], 'forms_good,forms_name,forms_imgs,forms_spec,forms_price,forms_total,forms_need,forms_sale,forms_number');

                $FindOrder['order_forms'] = $FindForms;

                $this->ShowAjax(1, '查询成功', $FindOrder);
            }else{
                $this->ShowAjax(2, '查无订单');
            }

            $this->ShowAjax(1, $Types);
        }
    }

    private function orderState($State){
        $Array = [
            '1' =>  '待支付',
            '2' =>  '待发货',
            '3' =>  '待收货',
            '4' =>  '已完成',
            '5' =>  '已评价',
            '6' =>  '待退款',
            '7' =>  '已退款',
            '8' =>  '已取消'
        ];

        return $Array[$State];
    }

}